package com.example.myapplication.socket;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class UdpSendThread extends Thread{

    public static final String TAG = "UdpSendThread";
    private static int i = 0; //静态变量，记录发送消息的次数
    private Handler handler = null;
    private String localIp;

    public UdpSendThread(Handler handler) {
        this.handler = handler;
    }
    @Override
    public void run() {
        try {
            Log.d(TAG, "*** run udp send ***");
            DatagramSocket socket = new DatagramSocket(48899); //自定端口号
            InetAddress address = InetAddress.getByName("192.168.0.255"); //经过当前 IP 创建相应的 InetAddress
            String data = "www.usr.cn";
            byte dataByte[] = data.getBytes(); //创建数据
            DatagramPacket packet = new DatagramPacket(dataByte, dataByte.length, address, 48899); //经过该数据建包
            socket.send(packet); //开始发送该包
            socket.close(); //其实对于发送方来讲不必关闭 socket，但为了防止没法预知的意外，建议关闭
            Log.i(TAG, "send done，data: " + data );

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
